
class GameStats():
    """跟踪游戏的统计信息"""

    def __init__(self, ai_settings):
        """初始化统计信息"""
        self.ai_settings = ai_settings
        self.reset_stats()

        # 游戏启动时处于非活动状态
        self.game_active = False

        # 在任何情况下都不应该重置最高得分
        self.high_score = 0

    def reset_stats(self):
        """初始化在游戏运行期间可能变化的统计信息"""
        self.ships_left = self.ai_settings.ship_limit
        self.score = 0
        self.level = 1

class GameStats():
    """跟踪游戏的统计信息"""

    def __init__(self, ai_settings):
        """初始化统计信息"""
        self.ai_settings = ai_settings
        self.reset_stats()

        # 游戏刚启动时处于活动状态
        self.game_active = False

    def reset_stats(self):
        """初始化在游戏运行期间可能变化的统计信息"""
        self.ships_left = self.ai_settings.ship_limit
        self.score = 0
        self.level = 1
        self.load_high_score()

    def save_high_score(self):
        """保存最高得分"""
        with open('score.txt', 'w') as file_object:
            file_object.write(str(self.high_score))
            file_object.close()

    def load_high_score(self):
        """加载最高得分"""
        try:
            with open('score.txt') as file_object:
                h_s = file_object.read()
                try:
                    self.high_score = int(h_s)
                except ValueError:
                    self.high_score = 0
                file_object.close()
        except FileNotFoundError:
            with open('score.txt', 'w') as file_object:
                file_object.close()